import axios from "./index";
import {
  type Product,
  type ProductListResponse,
  type ProductResponse,
  type Result,
} from "./types";

const productService = {
  async fetchProducts(params?: {
    page?: number;
    limit?: number;
    sortBy?: string;
    searchKey?: string;
  }): Promise<Result<ProductListResponse>> {
    let { data } = await axios.get<Result<ProductListResponse>>("/products", {
      params,
    });
    return data;
  },

  async fetchTopProducts(): Promise<Result<ProductResponse[]>> {
    let { data } = await axios.get<Result<ProductResponse[]>>("/products/top");
    return data;
  },

  async fetchProductById(productId: number): Promise<Result<ProductResponse>> {
    let { data } = await axios.get<Result<ProductResponse>>(
      `/products/get/${productId}`
    );
    return data;
  },

  async deleteProduct(productId: number): Promise<void> {
    let { data } = await axios.delete<void>(`/products/${productId}`);
    return data;
  },
  async updateProduct(productData: Product): Promise<void> {
    let { data } = await axios.put<void>(`/products`, productData);
    return data;
  },
};

export default productService;
